import 'package:dotted_line/dotted_line.dart';
import 'package:flutter/material.dart';
import 'package:gw_github_flutter/common/route_name.dart';
import 'package:gw_github_flutter/models/repo.dart';
import 'package:gw_github_flutter/tools/linearGradientColorsChangeView.dart';

class SystemRoute extends StatefulWidget {
  SystemRoute({Key? key}) : super(key: key);

  @override
  State<SystemRoute> createState() => _SystemRouteState();
}

class _SystemRouteState extends State<SystemRoute>
    with SingleTickerProviderStateMixin {
  List<Tab> tabs = [
    Tab(text: '关注'),
    Tab(text: '推按'),
    Tab(text: '视频'),
    Tab(text: '游戏'),
    Tab(text: '音乐'),
    Tab(text: '体育'),
    Tab(text: '生活'),
    Tab(text: '图片'),
  ];
  late TabController _controller;

  static const loadingTag = "##loading##"; //表尾标记
  // ignore: prefer_final_fields
  var _items = [
    Repo()
      ..name = RouteName.textFieldRoute + " 输入框"
      ..route_path = RouteName.textFieldRoute,
    Repo()
      ..name = RouteName.buttonRoute + " 按钮 单/复选框 "
      ..route_path = RouteName.buttonRoute,
    Repo()
      ..name = RouteName.imageViewRoute + " 图片 网络图片 "
      ..route_path = RouteName.imageViewRoute,
    Repo()
      ..name = RouteName.sliderRoute + " slider滑动进度条 自定义进度条 RangeSlider双向进度条"
      ..route_path = RouteName.sliderRoute,
    Repo()
      ..name = RouteName.chipRoute + " 标签"
      ..route_path = RouteName.chipRoute,
    Repo()
      ..name = RouteName.checkBoxAndSwitchRoute + " 标签"
      ..route_path = RouteName.checkBoxAndSwitchRoute,
    Repo()
      ..name = RouteName.notificationRoute + " 各种通知 监听"
      ..route_path = RouteName.notificationRoute,
    Repo()..name = loadingTag
  ];

  @override
  void initState() {
    super.initState();
    _controller = TabController(length: tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(

          /// AppBar是否显示在任务栏顶部
          primary: true,

          /// 标题
          title: Text(RouteName.systemRoute),

          /// 可以用这种方式设置渐变的AppBar
          flexibleSpace: LinearGradientColorsChangeView(
              rotation: Rotation.LR,
              colors: [Colors.cyan, Colors.blue, Colors.blueAccent]),

          /// 应用程序栏的工具栏部分是多么不透明。值1.0是完全不透明的，值0.0是完全透明的。
          toolbarOpacity: 1,

          /// AppBar底部透明度，设置方式同toolbarOpacity
          bottomOpacity: 0.5,
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.search),
              onPressed: () {},
            ),
            IconButton(
              icon: Icon(Icons.settings, color: Colors.white),
              onPressed: () {},
            ),
          ],

          /// bottom 一般就是TabBar，也可以是别的
          bottom:
              TabBar(tabs: tabs, isScrollable: true, controller: _controller)),
      body: _buildBody(),
    );
  }

  Widget _buildBody() {
    return ListView.separated(
      itemCount: _items.length,
      itemBuilder: (context, index) {
        if (_items[index].name == loadingTag) {
          return Container(
            padding: const EdgeInsets.all(16.0),
            alignment: Alignment.center,
            child: Text(
              "没有更多了",
              style: TextStyle(color: Colors.grey),
            ),
          );
        }
        return ListTile(
          title: Text(
            _items[index].name,
            style: TextStyle(fontSize: 13),
          ),
          subtitle: Text(_items[index].route_path ?? ""),
          onTap: () {
            Navigator.of(context).pushNamed(_items[index].route_path ?? "");
          },
        );
      },
      separatorBuilder: (context, index) {
        return const DottedLine(
          dashColor: Colors.red,
        );
        // return Divider(
        //   height: 0,
        // );
      },
    );
  }
}
